<?php
            
use app\model\Record; 
use app\model\Theme; 


/**
 * Desc:我的奖品
 * Time: 2021/3/23 8:32 上午
 */
class Prize_Page extends PluginMobilePage {

    function main($method)
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        $config = $_C;
        if ($fromtype=='mobile') {
            if (!$_G['ispost']){
                $themeid = isset($_P['themeid'])?$_P['themeid']:'';
                $theme = Theme::getThemeById($themeid);
                $return_client = get_defined_vars();
        return $this->view("lottery/index/flag", $return_client);
                return; //退出程序执行
            }else{
                $themeid = isset($_P['themeid'])?$_P['themeid']:'';
                $type = isset($_P['type'])?$_P['type']:'default';
                $theme = Theme::getThemeById($themeid);
                if (!$themeid || !$theme) {
                    return_json(1,'活动入口不正确');
                } else {
                    $fans = $this->checkuser();
                    if ($fans && $fans['uid']) {
                        $my_prize = LotteryModel::UserPrize($theme,$_C,$fans['uid']);
                        $record_list = LotteryModel::getRecordList($themeid,$_C,$fans['nickname']);//中奖记录
                        $open_red_package = $my_prize['open_red_package']?$my_prize['open_red_package']:false;
                        $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
                        $method_list = LotteryModel::getLotteryMethod($fans['uid'],$themeid,$_C,$themyrecord);


                        //TODO 弹出我的奖品
                        if ($type=='default'){
                            $myWinner_html = '<div class="myWinner" style="background: url('.$_C['yx_lt_convert_back_img'].') center top no-repeat;
                                            background-size: 100% auto;">'.PHP_EOL;
                            $myWinner_html .='<span class="winClose"></span>'.PHP_EOL.'<div class="winnerChose">'.PHP_EOL.'<span>中奖纪录</span>'.PHP_EOL.
                                '<span class="cur">我的奖品</span>'.PHP_EOL.'<span>抽奖攻略</span>'.PHP_EOL.'</div>'.PHP_EOL.'<div class="winerChosen">'.PHP_EOL;
                        }elseif ($type=='chance'){
                            $myWinner_html = '<div class="myWinner" style="background: url('.$_C['yx_lt_convert_back_img'].') center top no-repeat;
                                            background-size: 100% auto;">'.PHP_EOL.'<span class="winClose"></span>'.PHP_EOL.'<div class="winnerChose cur3">'.PHP_EOL.'<span>中奖纪录</span>'.PHP_EOL.
                                '<span >我的奖品</span>'.PHP_EOL.'<span class="cur">抽奖攻略</span>'.PHP_EOL.'</div>'.PHP_EOL.'<div class="winerChosen">'.PHP_EOL;
                        }

                        //  --中奖记录
                        $yx_peopleList_html = '<div class="yx_choseBar yx_peopleList dn">'.PHP_EOL.'<ul>'.PHP_EOL;

                        if ($record_list && !$config['yx_lt_show_flag_method']){
                            foreach ($record_list as $key => $val) {
                                $yx_peopleList_html .= '<li>'.PHP_EOL.'<div class="peoImg">'.PHP_EOL.'<img src="';
                                $yx_peopleList_html .= $val['avatar'].'">'.PHP_EOL.'</div>'.PHP_EOL.'<div class="peoName">'.PHP_EOL;
                                $yx_peopleList_html .= $val['username'].'</div>'.PHP_EOL.'<div class="peoInfor">'.PHP_EOL;
                                $yx_peopleList_html .= $val['tips'].'</div>'.PHP_EOL.'<div class="peoTime">'.PHP_EOL;
                                $yx_peopleList_html .= '<p>'.substr($val['created_at'],0,10).'</p>'.PHP_EOL.'<p>'.substr($val['created_at'],10,9).'</p>'.PHP_EOL.' </div>'.PHP_EOL.'</li>';
                            }
                        }
                        $yx_peopleList_html.= '</ul>'.PHP_EOL.'</div>'.PHP_EOL;

                        // --我的奖品  红包加抽奖奖品
                        if ($type=='default'){
                            $my_prize_html = '<div class="yx_choseBar yx_peopleList">'.PHP_EOL.'<div class="ticeketList">';
                        }elseif ($type=='chance'){
                            $my_prize_html = '<div class="yx_choseBar yx_peopleList dn">'.PHP_EOL.'<div class="ticeketList">';
                        }

                        //判断存在红包
                        if ($open_red_package) {
                            $my_prize_html .= '<div class="redBag">'.PHP_EOL.'<div class="redbagTxt">'.PHP_EOL.
                                '<p>'.$my_prize['red_package_info']['total_amount'].'元</p>'.PHP_EOL.
                                '<span>'. $my_prize['red_package_info']['btm_tips']. '</span>'.PHP_EOL.'</div><div class="redbagBtn">'.PHP_EOL;

                            if ($my_prize['red_package_info']['amount'] && $my_prize['red_package_info']['amount']>0 && $_S['autored']!=1){
                                $my_prize_html.= '<span>'.$my_prize['red_package_info']['amount'].'元待提现</span>';
                            }else{

                            }
                            if ($_S['autored']!=1){
                                $my_prize_html .= '<a id="convert_red_pack" data-cando="'. $my_prize['red_package_info']['can_get'] . '" class="getBag">'.
                                    $my_prize['red_package_info']['btn_txt']. '</a>';
                            }
                            $my_prize_html .= '</div>'.PHP_EOL.'</div>'.PHP_EOL;
                        }
//                    var_dump($_S['autored']);die();
                        //---默认奖品
                        $my_prize_html.='<ul>'.PHP_EOL;
                        if ($my_prize['prize_info']) {
                            foreach ($my_prize['prize_info'] as $t => $item) {
                                $my_prize_html.='<li>'.PHP_EOL.'<div class="ticketInfor">'.PHP_EOL.'<div class="ticketProduct">'.PHP_EOL.
                                    '<img src="'. $item['img'] . '">'.PHP_EOL.'</div>'.PHP_EOL.'<div class="getDate">'.PHP_EOL.'<p>'.$item['name'].'-'.$item['title'].'</p>'.PHP_EOL.
                                    '<span>'.$item['time'].'</span>'.PHP_EOL.'</div>'.PHP_EOL;
                                if ($item['type']=='red_envelope'){
                                    $item_send_status = LotteryModel::getRedPackageItemStatus($item['f_id'],$themeid,$fans['uid']);
                                    if ($item_send_status){
                                        if ($item_send_status['red_status']==3){
                                            if ($item_send_status['status']==1){
                                                $my_prize_html .= '<a class="small_color" data-status="'.$item['status'].'"  data-fid="'.$item['f_id'].'">已发放</a>'.PHP_EOL;
                                            }else{
                                                $my_prize_html .= '<a  data-status="'.$item['status'].'"  data-fid="'.$item['f_id'].'">未发放</a>'.PHP_EOL;
                                            }
                                        }else{
                                            $my_prize_html .= '<a  data-status="'.$item['status'].'"  data-fid="'.$item['f_id'].'">待提现</a>'.PHP_EOL;
                                        }
                                    }else{
                                        $my_prize_html .= '<a  data-status="'.$item['status'].'"  data-fid="'.$item['f_id'].'">待提现</a>'.PHP_EOL;
                                    }
                                }else{
                                    if ($item['status']==1) {
                                        if ($item['default_type']!=2) {
                                            $my_prize_html .= '<a  data-status="'.$item['status'].'" onclick="convert(this)" data-fid="'.$item['f_id'].'">'.$item['btn_txt'].'</a>'.PHP_EOL;
                                        }
                                    } else {
                                        if ($item['default_type']!=2) {
                                            if ($item['status']==3) {
                                                $my_prize_html .= '<a  data-status="'.$item['status'].'" onclick="convert(this)" data-fid="'.$item['f_id'].'">'.$item['btn_txt'].'</a>'.PHP_EOL;
                                            } elseif ($item['status']==4){
                                                $my_prize_html .= '<a class="small_color" data-status="'.$item['status'].'" data-fid="'.$item['f_id'].'">'.$item['btn_txt'].'</a>'.PHP_EOL;
                                            }else {
                                                $my_prize_html .= '<a  data-status="'.$item['status'].'" data-fid="'.$item['f_id'].'">'.$item['btn_txt'].'</a>'.PHP_EOL;
                                            }
                                        }
                                    }
                                }
                                $my_prize_html.= '</div>'.PHP_EOL.'</li>'.PHP_EOL;

                            }
                        }
                        $my_prize_html.='</ul>'.PHP_EOL;
                        $my_prize_html.='<div style="text-align: center;color: #a85912;font-size: 14px;">'.$config['yx_lt_myprize_tips'].'</div>'.PHP_EOL;
                        $my_prize_html.='<a  class="continue">'.$my_prize['btm_txt'].'</a>'.PHP_EOL.'</div>'.PHP_EOL.'</div>'.PHP_EOL;

                        // --抽奖攻略
                        if ($type=='default'){
                            $strategy_html = '<div class="yx_choseBar strategy dn">'.PHP_EOL.'<ul>'.PHP_EOL;
                        }elseif ($type=='chance'){
                            $strategy_html = '<div class="yx_choseBar strategy" style="display: block;">'.PHP_EOL.'<ul>'.PHP_EOL;
                        }
                        if ($method_list) {
                            foreach ($method_list as $K => $val) {
                                if ($val['type']!='timeset') {
                                    if ($val['type']=='order') {
                                        $strategy_html.= '<li>'.PHP_EOL.'<img src="'. $val['icon']. '">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL.
                                            '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                            '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL.
                                            '<a href="javascript::" onclick="show_register_info()" data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'" class="stratTag">'.
                                            $val['btn_txt'].'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                                    } elseif ($val['type']=='share' || $val['type']=='share_moments') {
                                        $strategy_html.= '<li>'.PHP_EOL.'<img src="'. $val['icon']. '">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL.
                                            '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                            '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL.
                                            '<a href="javascript::" onclick="show_share()" data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'" class="stratTag">'.
                                            $val['btn_txt'].'</a>'.PHP_EOL.'</li>'.PHP_EOL;

                                    } elseif ($val['type']=='helpFriend'){
                                        $strategy_html.= '<li class="li_f">'.PHP_EOL.'<img src="'. $val['icon']. '">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL.
                                            '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                            '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL.
                                            '<a href="javascript::" onclick="show_share()" data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'" class="stratTag">'.
                                            $val['btn_txt'].'</a>'.PHP_EOL.'</li><li class="f_list li_np" onclick="helpFriend()" data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'">'.

                                            $strategy_tmp = '<div>';
                                        $friend_help_list = LotteryModel::getFriendHelpInfo($themeid,$_C,$fans['uid']);
                                        if (count($friend_help_list['friend_list'])<1){
                                            for ($i=0;$i<=5;$i++){
                                                $strategy_tmp .=  '<div class="f_list_item"><div class="f_list_img_wrap"><span>+</span><img/></div></div>';
                                            }

                                        }else{
                                            foreach ($friend_help_list['friend_list'] as $key => $val){
                                                if ($key<=5){
                                                    if ($val && $val['avatar'])
                                                    {
                                                        $strategy_tmp .= '<div class="f_list_item"><div class="f_list_img_wrap"><span>+</span><img src="'.$val['avatar'].'"/></div></div>';
                                                    }else{
                                                        $strategy_tmp .=  '<div class="f_list_item"><div class="f_list_img_wrap"><span>+</span><img/></div></div>';
                                                    }
                                                }
                                            }
                                            $diff_num = 6-count($friend_help_list['friend_list']);
                                            for ($j=0;$j<$diff_num;$j++){
                                                $strategy_tmp .=  '<div class="f_list_item"><div class="f_list_img_wrap"><span>+</span><img/></div></div>';
                                            }
                                        }

                                        $strategy_tmp.= '</div><a href="javascript::"  data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'">查看全部</a></li>'.PHP_EOL;
                                        $strategy_html .= $strategy_tmp;
                                    }else {
                                        $strategy_html.= '<li>'.PHP_EOL.'<img src="'. $val['icon']. '">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL.
                                            '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                            '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL.
                                            '<a  data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'" class="stratTag">'.
                                            $val['btn_txt'].'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                                    }

                                } else {
                                    $strategy_html.= '<li>'.PHP_EOL.'<img src="'.$val['icon'].'">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL;
                                    $strategy_html.= '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                        '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL;
                                    //判断时间戳
                                    $temp_btn_txt = $val['btn_txt']?$val['btn_txt']:'后可领';
                                    $strategy_html.= '<a  data-status="'.$val['status'].'" data-type="'.$val['type'].'" data-timeset="'.$val['timeset'].'"
                               data-otherid="'.$val['other_id'].'" class="stratTag timeEnd12">'.PHP_EOL.'<div class="yx_yomibox mobile_color" data="'.$val['timeset'].'"></div>'.PHP_EOL.
                                        $temp_btn_txt.'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                                }
                            }
                        }
                        $strategy_html.='</ul>'.PHP_EOL.'</div>'.PHP_EOL;

                        $myWinner_html.= $yx_peopleList_html.$my_prize_html.$strategy_html.'</div>'.PHP_EOL.'</div>'.PHP_EOL;

                        $format_res = [
                            'myWinner_html'=>Theme::encode($myWinner_html)
                        ];
                        return_json(0,'操作成功',$format_res);
                    }
                }
            }
        } elseif ($fromtype=='wxapp') {
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);
            if (!$themeid || !$theme) {
                return_json(1,'活动入口不正确');
            } else {
                $fans = $this->checkuser();
                if ($fans && $fans['uid']) {
                    $my_prize = LotteryModel::UserPrize($theme,$_C,$fans['uid']);
                    $record_list = LotteryModel::getRecordList($themeid,$_C,$fans['nickname']);//中奖记录
                    $open_red_package = $my_prize['open_red_package']?$my_prize['open_red_package']:false;
                    $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
                    $method_list = LotteryModel::getLotteryMethod($fans['uid'],$themeid,$_C,$themyrecord);
                    if ($config['yx_lt_show_flag_method']){
                        $record_list = [];
                    }
                    $format_res = [
                        'my_prize'=>$my_prize,
                        'record_list'=>$record_list,
                        'open_red_package'=>$open_red_package,
                        'autored'=>$_S['autored'],
                        'method_list'=>$method_list,
                        'yx_lt_myprize_tips'=>$_C['yx_lt_myprize_tips'],
                    ];
                    return_json(0,'操作成功',$format_res);
                }else{
                    $format_res = [
                        'my_prize'=>[],
                        'record_list'=>[],
                        'open_red_package'=>[],
                        'autored'=>$_S['autored'],
                        'method_list'=>[],
                        'yx_lt_myprize_tips'=>$_C['yx_lt_myprize_tips'],
                    ];
                    return_json(0,'操作成功',$format_res);
                }
            }
        }
    }
}